package com.yx.yinshanjinniu.activitys;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.yx.yinshanjinniu.R;
import com.yx.yinshanjinniu.base.BaseActivity;
import com.yx.yinshanjinniu.net.ActionCallbackListener;
import com.yx.yinshanjinniu.utils.StringUtil;
import com.yx.yinshanjinniu.utils.ToastUtil;

public class FeedbackActivity extends BaseActivity {

    private TextView tv_counter;
    private EditText et_feedback;
    private TextView tv_submit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_feedback;
    }

    @Override
    protected void findViews() {
        tv_counter = $(R.id.tv_counter);
        et_feedback = $(R.id.et_feedback);
        tv_submit = $(R.id.tv_submit);
    }

    @Override
    protected void setListeners() {
        tv_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String feedback = et_feedback.getText().toString().trim();
                if (StringUtil.isEmpty(feedback)){
                    ToastUtil.showToast(mContext, "请填写反馈内容");
                }else {
                    // 这里实现提交逻辑
                    submitFeedback(feedback);
                }
            }
        });

        // 设置字数变化监听
        et_feedback.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                updateCharacterCount();
            }

            @Override
            public void afterTextChanged(Editable s) {}
        });
    }

    private void updateCharacterCount() {
        int currentLength = et_feedback.getText().length();
        tv_counter.setText(currentLength + "/100");

        // 可选：超过90字时变色提示
//        if (currentLength > 90) {
//            tv_counter.setTextColor(getResources().getColor(android.R.color.holo_red_dark));
//        } else {
//            tv_counter.setTextColor(getResources().getColor(android.R.color.darker_gray));
//        }
    }

    private void submitFeedback(String feedback) {
        mAppAction.toFeedBack(feedback, new ActionCallbackListener<Void>() {
            @Override
            public void onSuccess(Void data) {
                ToastUtil.showToast(mContext, "已收到您的反馈，感谢支持！");
                finish();
            }

            @Override
            public void onFailure(String errorCode, String message) {
                ToastUtil.showToast(mContext, message);
            }
        });
    }
}